package com.cuit.jifver.weibo.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cuit.jifver.weibo.data.ConfigUtil;
import com.cuit.jifver.weibo.data.Weibo;
import com.cuit.jifver.weibo.util.TextUtil;
import com.cuit.jifver.weibo.util.Constants;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewAct extends Activity {
	private static final String TAG = "jifver";
	private WebView webView;
	ConfigUtil configUtil = ConfigUtil.getInstance();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewact);
		
		webView = (WebView) findViewById(R.id.web);
		  Intent intent = this.getIntent();  
		     if (!intent.equals(null))  
		     {  
		         Bundle bundle = intent.getExtras();  
		         if (bundle != null)  
		         {  
		             if (bundle.containsKey("url"))  
		             {  
		                 String url = bundle.getString("url");  
		                WebSettings webSettings = webView.getSettings();  
		                 // 支持JavScript   
		                 webSettings.setJavaScriptEnabled(true);  
		                 webSettings.setSupportZoom(true);  
		                 webView.requestFocus();  
		                 webView.loadUrl(url);  
		                 Log.i(TAG, "WebView Starting....");  
		             }  
		         }  
		     }  
		     
		      //绑定java对象到JavaScript中，这样就能在JavaScript中调用java对象，实现通信。   
		      //这种方法第一个参数就是java对象，第二个参数表示java对象的别名，在JavaScript中使用   
		      webView.addJavascriptInterface(new JavaScriptInterface(), "Methods");  
		      WebViewClient client = new WebViewClient()  
		      {  
		          /** 
		           * 回调方法，当页面加载完毕后执行 
		           */  
		          @Override  
		          public void onPageFinished(WebView view, String url)  
		          {  
		        	 // System.out.println(TAG);
		              Log.i(TAG, "WebView onPageFinished");  
		              //执行获取授权码的JavaScript   
		              view.loadUrl("javascript:window.Methods.getHTML('<head>'+document.getElementsByTagName('body')[0].innerHTML+'</head>');");  
		              super.onPageFinished(view, url);  
		          }  
		          
		          @Override
		        public void onPageStarted(WebView view, String url,
		        		Bitmap favicon)
		        {
		        	  Log.e(TAG, "url-->"+url);
		        	  
		        	super.onPageStarted(view, url, favicon);
		        }
		          
		      };  
		      webView.setWebViewClient(client);
	}
	
	
	   class JavaScriptInterface  
	    {  
	       private static final String TAG = "MainActivity";  
	      public void getHTML(String html)  
	      {  
	           Log.i(TAG, html); 
	          // System.out.println("Html-->"+html);
	           String verifier = getVerifier(html);  
	           if (!TextUtil.isEmpty(verifier))  
	           {  
	              Log.i(TAG, "verifier:"+verifier);  
	              Intent intent = new Intent();
	              intent.setClass(WebViewAct.this, ShowAccessTokenAct.class);
	              Bundle bundle = new Bundle();
	              bundle.putString(Constants.oauth.OAUTH_VERIFIER, verifier);
	              intent.putExtras(bundle);
	              WebViewAct.this.startActivity(intent);
	              
	          }  
	     }  
	    
	       public String getVerifier(String html)  
		{
			String ret = "";
			String regEx = "授权码：[0-9]{6}";
			if (configUtil.getCurWeibo().equals(ConfigUtil.SOHUW))
			{
				Log.e(TAG, "config");
				regEx = ">[0-9]{8}";
			}
			if (configUtil.getCurWeibo().equals(ConfigUtil.WANGYIW))
			{
				regEx = "You have successfully authorized";
			}

			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(html);
			boolean result = m.find();
			if (result)
			{
				/*if (configUtil.getCurWeibo().equals(ConfigUtil.SOHUW))
				{
					ret = m.group(0).substring(1);

				}
				if (configUtil.getCurWeibo().equals(ConfigUtil.QQW))
				{
					ret = m.group(0).substring(4);
				}*/
				if (configUtil.getCurWeibo().equals(configUtil.WANGYIW))
				{
					ret = "success";
				}

			}
			return ret;
		}  
	  }  


}
